package com.github.permissiondog.cancanpbackend.servlet;

import com.github.permissiondog.cancanpbackend.model.Role;
import com.github.permissiondog.cancanpbackend.response.*;
import com.github.permissiondog.cancanpbackend.service.ApplicationService;
import com.github.permissiondog.cancanpbackend.service.TemplateService;
import com.github.permissiondog.cancanpbackend.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/applicationInfo")
public class ApplicationServlet extends BaseServlet{
    /**
     * 获取申请信息
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int applicationId;
        try {
            applicationId = Integer.parseInt(req.getParameter("applicationId"));
        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("参数错误").build());
            return;
        }

        try {
            var app = ApplicationService.getInstance().getApplicationById(applicationId);
            if (app == null) {
                json(res, BaseResponse.builder().ok(false).msg("申请 ID 不存在").build());
            }
            var progress = ApplicationService.getInstance().getProgressByApplicationId(applicationId);
            var template = TemplateService.getInstance().getTemplateById(app.getTemplateId());
            var approvers = TemplateService.getInstance().getApprovers(template.getId());
            var questions = TemplateService.getInstance().getQuestionsByTemplateId(template.getId());
            var applicant = UserService.getInstance().getUserById(app.getApplicantId());
            var status = ApplicationService.getInstance().getApplicationStatus(app.getId());
            var closed = ApplicationService.getInstance().isApplicationClosed(app.getId());

            var userLoggedIn = userLoggedIn(req);
            var currentApprover = ApplicationService.getInstance().getCurrentApprover(app.getId());
            var canApprove = userLoggedIn.getRole().equals(Role.APPROVER)
                    && currentApprover != null
                    && currentApprover.getId() == userLoggedIn.getId();

            json(res, ApplicationInfoResponse.builder().ok(true)
                    .template(template)
                    .application(app)
                    .progress(progress)
                    .approvers(approvers)
                    .questions(questions.stream().map(q -> Question.builder()
                            .question(q)
                            .answer(ApplicationService.getInstance().getAnswer(app.getId(), q.getId()))
                            .choices(TemplateService.getInstance().getChoices(q.getId()))
                            .build()).map(Question.class::cast).toList())
                    .status(status)
                    .closed(closed)
                    .applicant(applicant)
                    .canApprove(canApprove)
                    .build());

        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("获取错误").error(e.getMessage()).build());
        }

    }
}
